home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Snippets / MyNewCard 1.0.2 / MyNewCard.c < prev    next >
Encoding:
Text File  |  1995-12-01  |  7.5 KB  |  348 lines  |  [TEXT/CWIE]

  1. // MyNewCard
  2. // by Ken Long <kenlong@netcom.com>
  3. // updated for CW7 on 951201
  4.  
  5. /*
  6. From: kenlong@netcom.com (Ken Long)
  7. Subject: MyNewCard.c
  8.  
  9. Same as the one in my last post, here, except Matt Mora modified it to 
  10. display the color icon.  I had no experience with any "IconSuite" 
  11. headers, nor did I get any of them with any of the programming 
  12. environments I have (bummer).  So I got the proto from a Think Reference 
  13. example code copy and pasted it into Icon.h, which worked.  Evedently, 
  14. those "suite" protos are in some new Icons.h, because Matt had it 
  15. included.  I don't recall the inclusion being there before.
  16.  
  17. Anyway, I changed the zoomRects to a solid line (from a gray pat), and 
  18. centered the origin of the zoom at the top.
  19.  
  20. By the way, ZoomRect.c and .h are part of the Mac Prog Secrets source 
  21. (not where this came from, though).
  22.  
  23. You'll need a resource with an 'icl8' in a project with MacTraps and ANSI 
  24. to run this without substituting the .c file in "MyCard" project.
  25.  
  26. Enjoy!
  27.  
  28. -Ken-
  29. */
  30.  
  31. //• MyCard.c
  32.  
  33. //#include <icons.h>
  34.  
  35. #define        ONE         65536L
  36. #define        ZOOMSTEPS    16
  37.  
  38. WindowPtr        aboutW;
  39. Rect            aRect, cRect;
  40. char            aStr[255];
  41. EventRecord        aboutEvt;
  42. Boolean            aboutDone;
  43. Fixed    fract;
  44. SysEnvRec gMac;
  45.  
  46. //• Prototypes.
  47.  
  48. //pascal OSErr PlotIconID(Rect * theRect, short alignment,
  49. //        short transform, short theResID) 
  50. //        = {0x303C, 0x0500, 0xABC9};
  51.  
  52. void    drawabout (void);
  53. void    NewAbout (void);
  54. int        Blend (int i1, int i2);
  55. void        zoomrect (Rect *smallrect, Rect *bigrect, Boolean zoomup);
  56. void        ltog (Rect *r);
  57. void        zoomport (WindowPtr wind, Boolean up);
  58. void        centerwindow (WindowPtr wind, Rect *r);
  59. void        centerrect (Rect *r1, Rect *r2);
  60. void    InitMacintosh (void);
  61. void        main (void);
  62.  
  63. void drawabout (void) 
  64. {
  65.     //• Draw the contents of the "About..." window.
  66.     
  67.     Handle ourIcon;
  68.     Rect iconRect;
  69.     
  70.     
  71.     TextFont (0);
  72.     TextSize (18);
  73.  
  74.     ForeColor (blueColor);
  75.  
  76.     strcpy (aStr, "\pKenneth A. Long");    
  77.  
  78.     TextFace (condense);
  79.     MoveTo (((aboutW->portRect.right) - StringWidth ((StringPtr) aStr)) /2, 22);
  80.     DrawString ((StringPtr) aStr);
  81.  
  82.     TextSize (12);
  83.     
  84.     ForeColor (redColor);
  85.  
  86.     strcpy (aStr, "\pInventor - Industrial Artist - Writer");
  87.     TextFace (condense);
  88.     MoveTo (((aboutW->portRect.right) - StringWidth ((StringPtr) aStr)) /2, 40);
  89.     DrawString ((StringPtr) aStr);
  90.     TextFace (0);
  91.  
  92.     TextFont (3);
  93.     TextSize (9);
  94.  
  95.     ForeColor (blackColor);
  96.  
  97.     strcpy (aStr, "\pMacintosh Programmer (in training)");
  98.     MoveTo (((aboutW->portRect.right) - StringWidth ((StringPtr) aStr)) /2, 58);
  99.     DrawString ((StringPtr) aStr);
  100.  
  101.     strcpy (aStr, "\p");
  102.     MoveTo (((aboutW->portRect.right) - StringWidth ((StringPtr) aStr)) /2, 70);
  103.     DrawString ((StringPtr) aStr);
  104.  
  105.     ForeColor (redColor);
  106.  
  107.     strcpy (aStr, "\p\"Where there's a will, there's a way!\"");
  108.     MoveTo (((aboutW->portRect.right) -StringWidth ((StringPtr) aStr)) /2, 80);
  109.     DrawString ((StringPtr) aStr);
  110.  
  111.     ForeColor (blackColor);
  112.  
  113.     SetRect (&iconRect, 110, 90, 142, 122);
  114.  
  115.     if (gMac.systemVersion >= 0x0700) 
  116.     {
  117.         PlotIconID (&iconRect, 0, 0, 128);
  118.     } 
  119.     else 
  120.         {
  121.             ourIcon = GetResource ('ICN#', 128);
  122.             PlotIcon (&iconRect, ourIcon);
  123.     
  124.     }
  125.     
  126.     ReleaseResource (ourIcon);
  127.  
  128.     strcpy (aStr, "\pkenlong@netcom.com");
  129.     MoveTo (aboutW->portRect.left+4 , aboutW->portRect.bottom - 4);
  130.     DrawString ((StringPtr) aStr);
  131.  
  132.     strcpy (aStr, "\pkenlong@aol.com");
  133.     MoveTo (aboutW->portRect.right - 
  134.             StringWidth ((StringPtr) aStr) - 4 , 
  135.             aboutW->portRect.bottom - 4);
  136.             
  137.     DrawString ((StringPtr) aStr);
  138. }
  139.  
  140. void NewAbout (void) 
  141. {
  142.     GrafPtr        tempPort;
  143.  
  144.     //• Rather than using a dialog, just create a window to draw the
  145.     //• "About..." stuff in.
  146.     
  147.     GetPort (&tempPort);
  148.     InitCursor ();
  149.     SetRect (&aRect, 0, 0, 252, 144);
  150.     centerrect (&aRect, &qd.screenBits.bounds);
  151.     if (gMac.hasColorQD) 
  152.         aboutW = NewCWindow (nil, &aRect, "\pWindow", FALSE, 3, (WindowPtr) -1, FALSE, 0);
  153.     else
  154.         aboutW = NewWindow (nil, &aRect, "\pWindow", FALSE, 3, (WindowPtr) -1, FALSE, 0);
  155.     
  156.     SetPort (aboutW);
  157.     ForeColor (redColor);
  158.     zoomport (aboutW, TRUE);
  159.  
  160.     aboutDone = FALSE;
  161.     
  162.     //• do our own event-handling until the user either clicks the 
  163.     //• mouse, or presses a key on the keyboard
  164.  
  165.     do
  166.     {
  167.         if (GetNextEvent (everyEvent, &aboutEvt)) 
  168.         {
  169.             switch (aboutEvt.what) 
  170.             {
  171.                 case updateEvt:        
  172.                 {
  173.                     BeginUpdate (aboutW);
  174.                     drawabout ();
  175.                     EndUpdate (aboutW);
  176.                 }
  177.                 break;
  178.                                             
  179.                 case keyDown:
  180.                 case autoKey:
  181.                 case mouseDown:
  182.                     aboutDone = TRUE;
  183.                 break;
  184.                                             
  185.                 default:
  186.                 break;
  187.             }
  188.         }
  189.     }while (!aboutDone);
  190.     HideWindow (aboutW);
  191.     zoomport (aboutW, FALSE);
  192.     DisposeWindow (aboutW);
  193.     SetPort (tempPort);
  194. }
  195.  
  196. int Blend (int i1, int i2) 
  197. {
  198.     Fixed    smallFix, bigFix, tempFix;
  199.  
  200.     smallFix = ONE * i1;
  201.     bigFix = ONE * i2;
  202.     tempFix = FixMul (fract, bigFix) +FixMul (ONE-fract, smallFix);
  203.     return (FixRound (tempFix));
  204. }
  205.  
  206. void zoomrect (Rect *smallrect, Rect *bigrect, Boolean zoomup) 
  207. {
  208.     Fixed        factor;
  209.     Rect        rect1, rect2, rect3, rect4;
  210.     GrafPtr        savePort, deskPort;
  211.     int            i;
  212.     long        tm;
  213.  
  214.     GetPort (&savePort);
  215.     OpenPort (deskPort = (GrafPtr) NewPtr (sizeof (GrafPort)));
  216.     InitPort (deskPort);
  217.     SetPort (deskPort);
  218.     PenPat (&qd.gray);        //• Original, comment for black zoom.
  219. //    PenPat (&qd.black);        //• Uncomment for black zoom.
  220.     PenMode (notPatXor);    //• Original, comment for black zoom.
  221. //    PenMode (patXor);        //• Uncomment for black zoom.
  222.     if (zoomup) 
  223.     {
  224.         rect1 = *smallrect;
  225.         factor = FixRatio (6, 5);
  226.         fract = FixRatio (541, 10000);
  227.     }
  228.     else
  229.         {
  230.             rect1 = *bigrect;
  231.             factor = FixRatio (5, 6);
  232.             fract = ONE;
  233.     }
  234.     rect2 = rect1;
  235.     rect3 = rect1;
  236.     FrameRect (&rect1);
  237.     for (i = 1; i <= ZOOMSTEPS; i++) 
  238.     {
  239.         rect4.left = Blend (smallrect->left, bigrect->left);
  240.         rect4.right = Blend (smallrect->right, bigrect->right);
  241.         rect4.top = Blend (smallrect->top, bigrect->top);
  242.         rect4.bottom = Blend (smallrect->bottom, bigrect->bottom);
  243.         FrameRect (&rect4);
  244.         FrameRect (&rect1);
  245.         rect1 = rect2;
  246.         rect2 = rect3;
  247.         rect3 = rect4;
  248.         fract = FixMul (fract, factor);
  249.         tm = TickCount ();
  250.         while (tm == TickCount ());
  251.         tm = TickCount ();
  252.         while (tm == TickCount ());
  253.     }
  254.     FrameRect (&rect1);
  255.     FrameRect (&rect2);
  256.     FrameRect (&rect3);
  257.     ClosePort (deskPort);
  258.     DisposPtr ((Ptr) deskPort);
  259.     PenNormal ();
  260.     SetPort (savePort);
  261. }
  262.  
  263. void ltog (Rect *r) 
  264. {
  265.     Point    p1, p2;
  266.  
  267.     p1 = topLeft (*r);
  268.     p2 = botRight (*r);
  269.     LocalToGlobal (&p1);
  270.     LocalToGlobal (&p2);
  271.     Pt2Rect (p1, p2, r);
  272. }
  273.  
  274. void zoomport (WindowPtr wind, Boolean up) 
  275. {
  276.     Rect    r1, r2, r3;
  277.     
  278.     SetPort (wind);
  279.     SetRect (&r1, qd.screenBits.bounds.right / 2, 0,
  280.                   qd.screenBits.bounds.right / 2, 0);
  281.  
  282.     r3 = wind->portRect;
  283.     r2 = r3;
  284.     InsetRect (&r2, (r3.right - r3.left + 20) /2, 
  285.                   (r3.bottom - r3.top + 20) /2);
  286.     
  287.     ltog (&r2);
  288.     ltog (&r3);
  289.     
  290.     if (up) 
  291.     {
  292.         zoomrect (&r1, &r2, TRUE);
  293.         zoomrect (&r2, &r3, TRUE);
  294.         ShowWindow (wind);
  295.         SetPort (wind);
  296.     }
  297.     else
  298.         {
  299.             HideWindow (wind);
  300.             zoomrect (&r2, &r3, FALSE);
  301.             zoomrect (&r1, &r2, FALSE);
  302.     }
  303. }
  304.     
  305. void centerwindow (WindowPtr wind, Rect *r) 
  306. {
  307.     Rect    r2;
  308.     
  309.     r2 = wind->portRect;
  310.     MoveWindow     (wind, 
  311.                  ((r->right-r->left) - 
  312.                   (r2.right-r2.left)) /2 - r->left, 
  313.                  ((r->bottom-r->top) - 
  314.                   (r2.bottom-r2.top)) /2 - r->top, 
  315.                    false);
  316. }
  317.  
  318. void centerrect (Rect *r1, Rect *r2) 
  319. {
  320.     OffsetRect     (r1, 
  321.                  ((r2->right  - r2->left) -
  322.                  (r1->right  - r1->left)) / 2 - r1->left, 
  323.                  ((r2->bottom - r2->top)  -
  324.                  (r1->bottom - r1->top)) / 2 - r1->top);
  325. }
  326.  
  327.  
  328. void InitMacintosh (void) 
  329. {
  330.     MaxApplZone ();
  331.     
  332.     InitGraf (& (qd.thePort));
  333.     InitFonts ();
  334.     FlushEvents (everyEvent, 0);
  335.     InitWindows ();
  336.     InitMenus ();
  337.     TEInit ();
  338.     InitDialogs (0L);
  339.     InitCursor ();
  340.     SysEnvirons (1, &gMac);
  341. }
  342.  
  343. void main (void) 
  344. {
  345.     InitMacintosh ();
  346.     NewAbout ();
  347. }
  348.